<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<div id="required_fields_message">Fields in red are required</div>
<ul id="error_message_box"></ul>
<form action="<s:url action="update"/>" method="post" accept-charset="utf-8" id="item_form"><fieldset id="item_basic_info">
<legend>Item Information</legend>
<jsp:useBean id="dateValue" class="java.util.Date" />
<jsp:setProperty name="dateValue" property="time" value="${product.lastUpdated}" />
 <input type="hidden" name="id" value="${product.id}"/>
<div class="field_row clearfix">
<label for="name" class="required wide">Item Name:</label>	<div class='form_field'>
	<input type="text" name="name" value="${product.name}" id="name"  />	</div>
</div>


<div class="field_row clearfix">
    <label for="name" class="required wide">Sale price:</label>
    <div class='form_field'>
	    <input type="text" name="salePrice" value="${product.salePrice}" id="salePrice"  />	(Updated :${dateValue})
    </div>
</div>

<div class="field_row clearfix">
<label for="supplier" class="required wide">Category:</label>
<div class='form_field'>
	      <select name="category">
         <option value="0" selected="selected">None</option>
    	<c:forEach var="item" items="${listCategory}">
         <option value="${item.id}" <c:if test="${product.category.id == item.id }">selected="selected"</c:if> >${item.name}</option>
         </c:forEach>
        </select>
	</div>
</div>

<div class="field_row clearfix">
<label for="description" class="wide">Description:</label>	<div class='form_field'>
	<textarea name="description" cols="17" rows="5" id="description" >${product.description}</textarea>	</div>
</div>


<input type="submit" name="submit" value="Submit" id="submit" class="submit_button float_right"  /></fieldset>
</form>
<script type='text/javascript'>

//validation and submit handling
$(document).ready(function()
{
	$( "#category" ).autocomplete({
		source: "${pageContext.request.contextPath}/items/suggest_category",
		delay: 10,
		autoFocus: false,
		minLength: 0
	});

	var submitting = false;

	$('#item_form').validate({
		submitHandler:function(form)
		{
			if (submitting) return;
			submitting = true;
			$(form).mask("Please wait...");

			$(form).ajaxSubmit({
			success:function(response)
			{

				submitting = false;
				tb_remove();
				post_item_form_submit(response);
			},
			dataType:'json'
		});

		},
		errorLabelContainer: "#error_message_box",
 		wrapper: "li",
		rules:
		{
			name:"required",
			category:"required",
			cost_price:
			{
				required:true,
				number:true
			},

			unit_price:
			{
				required:true,
				number:true
			},
			tax_percent:
			{
				required:true,
				number:true
			},
			quantity:
			{
				required:true,
				number:true
			},
			reorder_level:
			{
				required:true,
				number:true
			}
   		},
		messages:
		{
			name:"Item Name is a required field",
			category:"Category is a required field",
			cost_price:
			{
				required:"Cost Price is a required field",
				number:"Cost price must be a number"
			},
			unit_price:
			{
				required:"Selling Price is a required field",
				number:"Unit price must be a number"
			},
			tax_percent:
			{
				required:"Tax Percent is a required field",
				number:""
			},
			quantity:
			{
				required:"Quantity is a required field. Please Close ( X ) to cancel",
				number:"Quantity must be a number"
			},
			reorder_level:
			{
				required:"Reorder level is a required field",
				number:"Reorder level must be a number"
			}

		}
	});
});
</script>